<?php 
namespace Home\Controller;

use Think\Controller;

class CollectController extends Controller
{
	public function index()
	{
		if(empty($_SESSION['User'])){
			$this->error('请先登录！');
		}

		$uid = $_SESSION['User']['uid'];
		$col = M('collect');
		//查询该用户已收藏的商品信息
		$list=$col->field('goods.id,goods.gname,goods.price,goods.img,goods.sales')->join('goods on goods.id = collect.gid')->where('collect.uid='.$uid)->select();
		// var_dump($list);
		$this->assign('list',$list);
		$this->display();
	}

	public function collect()
	{
		if(empty($_SESSION['User'])){
			$this->error('请先登录！');
		}

		$list=[];
		$list['uid']=$_SESSION['User']['uid'];
		$list['gid']=$_GET['gid'];
		$col = M('collect');

		//判断是否已经收藏
		$fin = $col->where(array('uid'=>$list['uid'],'gid'=>$list['gid']))->select();
		if($fin){
			echo '2';//已收藏
		}else{
			if($col->create($list)){
				$result=$col->add();
				echo '1';//未收藏
				die;
			}
		}

			
		
	}

	public function cancel()
	{		
		$col = M('collect');
		$list['uid']=$_SESSION['User']['uid'];
		$list['gid']=$_GET['id'];
		// var_dump($list);
		if($col->create($list)){

			if($col->where($list)->delete()){
				
				$this->success('取消收藏成功!');
			}else{
				// echo $col->getLastSql();die;
				$this->error('取消收藏失败!');
			}
		}
	}

	public function comment()
	{
		$this->display();
	}

	public function foot()
	{
		// var_dump($_SESSION);
		$uid = $_SESSION['User']['uid'];
		$foot = M('foot');
		//看过的商品
		$flist = $foot->field('goods.id,goods.img,goods.gname,goods.price,foot.time')->join('goods on goods.id = foot.gid')->where('uid='.$uid)->select();
		if(empty($flist)){
			$this->error('你还没有看过任何商品╮(╯▽╰)╭');
		}else{
			//看的时间
			$ftime = $foot->field('time')->where('uid='.$uid)->select();
			$fotime = [];
			//组成新数组
			foreach ($ftime as $k => $v) {
				$fotime[$k] = $v['time'];
				// var_dump($v);
			}
			//删除重复值
			$fotime = array_unique($fotime);
			//组成新数组
			$list = [];
			foreach ($fotime as $v) {
				foreach ($flist as $k => $vo) {
					if($vo['time']==$v){
						$list[$v][$k]=$vo;
					}
				}
			}

			// var_dump($list);
			// var_dump($fotime);
			// var_dump($flist);die;
			$this->assign('list',$list);
			$this->display();
		}
		
	}

	public function news()
	{
		$this->display();
	}
}